﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DTO;
using Business;

namespace QuanLyNhaSach
{
    public partial class LapHoaDonBanSach : Form
    {
        private ErrorProvider MaKhachKhachHangErrorProvider;
        private ErrorProvider TienNoErrorProvider;
        private ErrorProvider TienNoMoiErrorProvider;
        public LapHoaDonBanSach()
        {
            InitializeComponent();
        }
       /* public void InitializeMyToolBar()
        {
            // Create and initialize the ToolBar and ToolBarButton controls.
          ToolBar  toolBar1 = new ToolBar();
            ToolBarButton toolBarButton1 = new ToolBarButton();
            ToolBarButton toolBarButton2 = new ToolBarButton();
            ToolBarButton toolBarButton3 = new ToolBarButton();

            // Set the Text properties of the ToolBarButton controls.
            toolBarButton1.Text = "Open";
            toolBarButton2.Text = "Save";
            toolBarButton3.Text = "Print";

            // Add the ToolBarButton controls to the ToolBar.
            toolBar1.Buttons.Add(toolBarButton1);
            toolBar1.Buttons.Add(toolBarButton2);
            toolBar1.Buttons.Add(toolBarButton3);

            // Add the event-handler delegate.
          /*  toolBar1.ButtonClick += new ToolBarButtonClickEventHandler(
               this.toolBar1_ButtonClick);

            // Add the ToolBar to the Form.
            Controls.Add(toolBar1);
        }*/

        private void UpdateSoThuTuDataGridViewHoaDonBanSach()
        {
            for (int i = 0; i < dgvHoaDonBanSach.Rows.Count - 1; i++)
            {
                dgvHoaDonBanSach.Rows[i].Cells[0].Value = (i + 1).ToString();
            }
        }
        private void SetUpdgvLapPhieuNhapSachPhieuNhapSach()
        {
           
           
            
            dgvHoaDonBanSach.ColumnCount = 7;
            DataGridViewCellStyle columnHeaderStyle = new DataGridViewCellStyle();
            columnHeaderStyle.BackColor = Color.Aqua;
            columnHeaderStyle.Font = new Font("Vernada", 10, FontStyle.Bold);
            dgvHoaDonBanSach.ColumnHeadersDefaultCellStyle = columnHeaderStyle;

            //set columnHeadername
            dgvHoaDonBanSach.Columns[0].Name = "STT";
            dgvHoaDonBanSach.Columns[1].Name = "Mã Sách";
            dgvHoaDonBanSach.Columns[2].Name = "Số Lượng Tồn";
            dgvHoaDonBanSach.Columns[3].Name = "Số Lượng Bán";
            dgvHoaDonBanSach.Columns[4].Name = "Đơn Giá Bán";
            dgvHoaDonBanSach.Columns[5].Name = "Tồn Cuối";
            dgvHoaDonBanSach.Columns[6].Name = "Tiền";


            DataGridViewComboBoxColumn comboboxcolumnTenSach = new DataGridViewComboBoxColumn();
            comboboxcolumnTenSach.Name = "Tên Sách";
            comboboxcolumnTenSach.DataSource = SachService.GetAllSach();
            comboboxcolumnTenSach.DisplayMember = SachColumns.TenSach;
            comboboxcolumnTenSach.ValueMember = SachColumns.MaSach;

            DataGridViewComboBoxColumn comboboxcolumnTheLoai = new DataGridViewComboBoxColumn();
            comboboxcolumnTheLoai.Name = "Thể Loại";
            comboboxcolumnTheLoai.DataSource = TheLoaiService.GetAllTheLoai();
            comboboxcolumnTheLoai.DisplayMember = TheLoaiColumns.TenTheLoai;
            comboboxcolumnTheLoai.ValueMember = TheLoaiColumns.MaTheLoai;

            dgvHoaDonBanSach.Columns.Insert(2, comboboxcolumnTenSach);
            dgvHoaDonBanSach.Columns.Insert(3, comboboxcolumnTheLoai);

            dgvHoaDonBanSach.Columns[0].ReadOnly = true;
            dgvHoaDonBanSach.Columns[2].ReadOnly = true;
            dgvHoaDonBanSach.Columns[3].ReadOnly = true;
            dgvHoaDonBanSach.Columns[4].ReadOnly = true;
            dgvHoaDonBanSach.Columns[7].ReadOnly = true;
            dgvHoaDonBanSach.Columns[8].ReadOnly = true;

            dgvHoaDonBanSach.AllowUserToAddRows = true;
            dgvHoaDonBanSach.AllowUserToDeleteRows =true;
            dgvHoaDonBanSach.MultiSelect = false;

           

            

        }
        private void InitMaKhachHangErrorProvider()
        {
            MaKhachKhachHangErrorProvider = new ErrorProvider();
            MaKhachKhachHangErrorProvider.SetIconAlignment(this.txtMaKhachHang, ErrorIconAlignment.MiddleRight);
            MaKhachKhachHangErrorProvider.SetIconPadding(this.txtMaKhachHang, 2);
            MaKhachKhachHangErrorProvider.BlinkRate = 1000;
            MaKhachKhachHangErrorProvider.BlinkStyle = ErrorBlinkStyle.AlwaysBlink;          
        }
        private void InitTienNoErrorProvider()
        {
            TienNoErrorProvider = new ErrorProvider();
            TienNoErrorProvider.SetIconAlignment(this.txtTienNo, ErrorIconAlignment.MiddleRight);
            TienNoErrorProvider.SetIconPadding(this.txtTienNo, 2);
            TienNoErrorProvider.BlinkRate = 1000;
            TienNoErrorProvider.BlinkStyle = ErrorBlinkStyle.AlwaysBlink;
        }
        private void InitTienNoMoiErrorProvider()
        {
            TienNoMoiErrorProvider = new ErrorProvider();
            TienNoMoiErrorProvider.SetIconAlignment(this.txtNoMoi, ErrorIconAlignment.MiddleRight);
            TienNoMoiErrorProvider.SetIconPadding(txtNoMoi, 2);
            TienNoMoiErrorProvider.BlinkRate = 1000;
            TienNoMoiErrorProvider.BlinkStyle = ErrorBlinkStyle.AlwaysBlink;
        }
       
        
        private void LapPhieuNhapSach_Load(object sender, EventArgs e)
        {
            SetUpdgvLapPhieuNhapSachPhieuNhapSach();
            InitMaKhachHangErrorProvider();
            InitTienNoErrorProvider();
            InitTienNoMoiErrorProvider();
            
        }

        private void dgvHoaDonBanSach_CellEndEdit(object sender, DataGridViewCellEventArgs e)
        {
            if (e.ColumnIndex == 1)
            {
                if (dgvHoaDonBanSach.Rows[e.RowIndex].Cells[1].Value != null)
                {
                    int masach;
                    if (int.TryParse(dgvHoaDonBanSach.Rows[e.RowIndex].Cells[1].Value.ToString(), out masach))
                    {
                        dgvHoaDonBanSach.Rows[e.RowIndex].Cells[1].ErrorText = "";
                        if (SachService.KiemTraTonTaiMaSach(masach))
                        {
                            dgvHoaDonBanSach.Rows[e.RowIndex].Cells[1].ErrorText = "";
                            SachDTO sachDTO = new SachDTO();
                            sachDTO = SachService.GetSachByID(masach);
                            dgvHoaDonBanSach.Rows[e.RowIndex].Cells[2].Value = sachDTO.MaSach;
                            dgvHoaDonBanSach.Rows[e.RowIndex].Cells[3].Value = sachDTO.MaTheLoai;
                            dgvHoaDonBanSach.Rows[e.RowIndex].Cells[4].Value = sachDTO.SoLuongTon;
                            dgvHoaDonBanSach.Rows[e.RowIndex].Cells[6].Value = sachDTO.DonGia;

                        }
                        else
                        {
                            dgvHoaDonBanSach.Rows[e.RowIndex].Cells[1].ErrorText = "Mã sách không tồn tại";
                        }
                    }
                    else
                    {
                        dgvHoaDonBanSach.Rows[e.RowIndex].Cells[1].ErrorText = "Mã sách phải là số";
                    }
                }
                else
                {
                    dgvHoaDonBanSach.Rows[e.RowIndex].Cells[1].ErrorText = "";
                }
            }
            if (e.ColumnIndex == 5)
            {
                if (dgvHoaDonBanSach.Rows[e.RowIndex].Cells[5].Value != null)
                {
                    if (dgvHoaDonBanSach.Rows[e.RowIndex].Cells[1].Value != null)
                    {
                        int soluong;
                        if (int.TryParse(dgvHoaDonBanSach.Rows[e.RowIndex].Cells[5].Value.ToString(), out soluong))
                        {
                            //MessageBox.Show(
                            dgvHoaDonBanSach.Rows[e.RowIndex].Cells[5].ErrorText = "";
                            if (dgvHoaDonBanSach.Rows[e.RowIndex].Cells[4].Value != null)
                            {
                                int soluongton;
                                if (int.TryParse(dgvHoaDonBanSach.Rows[e.RowIndex].Cells[4].Value.ToString(), out soluongton))
                                {

                                    int soluongtonsauban = soluongton - soluong;
                                    dgvHoaDonBanSach.Rows[e.RowIndex].Cells[7].Value = soluongtonsauban;
                                    ThamSoDTO thamso = new ThamSoDTO();
                                    thamso = ThamSoService.GetThamSoByID(4);

                                    if (soluongtonsauban < thamso.GiaTri)
                                    {
                                        dgvHoaDonBanSach.Rows[e.RowIndex].Cells[7].ErrorText = "Chỉ bán đầu sách có số lượng tồn sau khi bán ít nhất là " + thamso.GiaTri.ToString();
                                       
                                    }
                                    else
                                    {
                                        dgvHoaDonBanSach.Rows[e.RowIndex].Cells[7].ErrorText = "";
                                        if (dgvHoaDonBanSach.Rows[e.RowIndex].Cells[6].Value != null)
                                        {
                                            float dongia;
                                            dongia = float.Parse(dgvHoaDonBanSach.Rows[e.RowIndex].Cells[6].Value.ToString());
                                            float tien = soluong * dongia;
                                            dgvHoaDonBanSach.Rows[e.RowIndex].Cells[8].Value = tien;

                                            float nomoi = int.Parse(txtNoMoi.Text) + tien;
                                            txtNoMoi.Text = nomoi.ToString();

                                          



                                        }

                                    }

                                }
                            }
                        }
                        else
                        {
                            dgvHoaDonBanSach.Rows[e.RowIndex].Cells[5].ErrorText = "Số lượng phải là số";
                        }
                    }
                    else
                    {
                        dgvHoaDonBanSach.Rows[e.RowIndex].Cells[5].ErrorText = "Chưa nhập mã sách";
                       
                       
                    }


                }
                else
                {
                    dgvHoaDonBanSach.Rows[e.RowIndex].Cells[5].ErrorText = string.Empty;
                }
            }
        }

        private void txtMaKhachHang_TextChanged(object sender, EventArgs e)
        {
          /*  QuanLyTheLoai frm = new QuanLyTheLoai();
            frm.ShowDialog();
           //set thuoc tinh dialogresult cua button trên form là OK
            if (frm.DialogResult == DialogResult.OK)
            {
                MessageBox.Show("thanh cong");

            }
            else
            {

            }
            frm.Dispose();*/

            if (!string.IsNullOrEmpty(txtMaKhachHang.Text))
            {
                int makhachhang;
                if (int.TryParse(txtMaKhachHang.Text, out makhachhang))
                {

                    //kiem tra ma khach hang ton tai
                    if (KhachHangService.KiemTraTonTaiMaKhachHang(makhachhang))
                    {
                        MaKhachKhachHangErrorProvider.SetError(txtMaKhachHang,string.Empty);
                        KhachHangDTO khachhangDTO = new KhachHangDTO();
                        khachhangDTO = KhachHangService.GetKhachHangByID(makhachhang);
                        txtHoTen.Text = khachhangDTO.TenKhachHang;
                        txtTienNo.Text = khachhangDTO.TienNo.ToString();
                        txtNoMoi.Text = khachhangDTO.TienNo.ToString();
                       
                    }
                    else
                    {
                        MaKhachKhachHangErrorProvider.SetError(txtMaKhachHang, "Mã khách hàng không tồn tại");
                    }

                }
                else
                {
                    MaKhachKhachHangErrorProvider.SetError(txtMaKhachHang, "Mã khách hàng phải là số");
                    txtTienNo.Text = "";
                    txtHoTen.Text = "";                  
                }
            }
            else
            {
                MaKhachKhachHangErrorProvider.SetError(txtMaKhachHang,string.Empty);
                txtTienNo.Text = "";
                txtHoTen.Text = "";
               
            }
        }

        private void txtMaKhachHang_Validated(object sender, EventArgs e)
        {
           /* if (txtMaKhachHang.Text.Length > 0)
            {
                int makhachhang;
                if (int.TryParse(txtMaKhachHang.Text, out makhachhang))
                {
                    MaKhachKhachHangErrorProvider.SetError(txtMaKhachHang, string.Empty);
                }
                else
                {
                    MaKhachKhachHangErrorProvider.SetError(txtMaKhachHang, "Mã khách hàng phải là số");
                }
            }
            else
            {
                MaKhachKhachHangErrorProvider.SetError(txtMaKhachHang, string.Empty);
            }*/
        }

        private void txtTienNo_TextChanged(object sender, EventArgs e)
        {

            if (!string.IsNullOrEmpty(txtTienNo.Text))
            {
                int tienno;
                if (int.TryParse(txtTienNo.Text, out tienno))
                {
                    ThamSoDTO thamso = new ThamSoDTO();
                    thamso = ThamSoService.GetThamSoByID(3);
                    if (tienno > thamso.GiaTri)
                    {
                        TienNoErrorProvider.SetError(txtTienNo, "Tiền nợ không vượt được vượt quá " + thamso.GiaTri.ToString());
                    }
                    else
                    {
                        TienNoErrorProvider.SetError(txtTienNo, string.Empty);
                    }
                }
                else
                {
                    TienNoErrorProvider.SetError(txtTienNo, string.Empty);
                }
            }
            else
            {
                TienNoErrorProvider.SetError(txtTienNo, string.Empty);
            }
            
           
        }
        public bool KiemTraDongDuLieuNhap(int i)
        {
            bool kq = true;
          //  if (dgvHoaDonBanSach.Rows.Count-1 > 0)
          //   {
           //    for (int i = 0; i < dgvHoaDonBanSach.Rows.Count - 1; i++)
          //      {
                    if (dgvHoaDonBanSach.Rows[i].Cells[1].Value != null)
                    {
                        int masach;
                        if (int.TryParse(dgvHoaDonBanSach.Rows[i].Cells[1].Value.ToString(), out masach))
                        {
                            if (SachService.KiemTraTonTaiMaSach(masach))
                            {
                               
                                if (dgvHoaDonBanSach.Rows[i].Cells[6].Value != null)
                                {
                                    if (dgvHoaDonBanSach.Rows[i].Cells[7].Value != null)
                                    {
                                        ThamSoDTO thamso = new ThamSoDTO();
                                        thamso = ThamSoService.GetThamSoByID(4);
                                        int soluongsaukhiban = int.Parse(dgvHoaDonBanSach.Rows[i].Cells[7].Value.ToString());
                                        if (soluongsaukhiban < thamso.GiaTri)
                                        {
                                            kq = false;
                                        }

                                        
                                    }
                                }
                                else
                                {
                                    kq = false;
                                }
                            }
                            else
                            {
                                kq = false;
                            }
                        }
                        else
                        {
                            kq = false;
                        }

                    }
                    else
                    {
                        kq = false;
                    }
                    
              //  }
           // }
            return kq;
        }
        public bool KiemTraToanBoDuLieuNhap()
        {
            bool kq = true;
            for (int i = 0; i < dgvHoaDonBanSach.Rows.Count - 1; i++)
            {
                if (!KiemTraDongDuLieuNhap(i))
                {
                    kq = false;
                }
            }
            return kq;
        }

        private void dgvHoaDonBanSach_RowsAdded(object sender, DataGridViewRowsAddedEventArgs e)
        {
            UpdateSoThuTuDataGridViewHoaDonBanSach();
        }

        private void dgvHoaDonBanSach_RowsRemoved(object sender, DataGridViewRowsRemovedEventArgs e)
        {
            UpdateSoThuTuDataGridViewHoaDonBanSach();
            MessageBox.Show(e.RowIndex.ToString());
           
           
            
            
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void dgvHoaDonBanSach_RowValidating(object sender, DataGridViewCellCancelEventArgs e)
        {
           // MessageBox.Show(e.RowIndex.ToString());
           
               
                if (KiemTraToanBoDuLieuNhap())
                {

                    btnSave.Enabled = true;
                }
                else
                {
                    btnSave.Enabled = false;
                }
           
        }

        private void dgvHoaDonBanSach_UserDeletingRow(object sender, DataGridViewRowCancelEventArgs e)
        {
            if (dgvHoaDonBanSach.Rows[e.Row.Index].IsNewRow == false)
            {
                if (KiemTraDongDuLieuNhap(e.Row.Index))
                {
                    float tien = float.Parse(dgvHoaDonBanSach.Rows[e.Row.Index].Cells[8].Value.ToString());
                    float nomoi = float.Parse(txtNoMoi.Text) - tien;
                   
                    txtNoMoi.Text = nomoi.ToString();
                }
            }
        }

        private void btnXoa_Click(object sender, EventArgs e)
        {
            if (dgvHoaDonBanSach.Rows[ dgvHoaDonBanSach.SelectedCells[0].RowIndex].IsNewRow==false)
            {
                if (KiemTraDongDuLieuNhap(dgvHoaDonBanSach.SelectedCells[0].RowIndex))
                {
                    float tien = float.Parse(dgvHoaDonBanSach.Rows[dgvHoaDonBanSach.SelectedCells[0].RowIndex].Cells[8].Value.ToString());
                    float nomoi = float.Parse(txtNoMoi.Text) - tien;
                    txtNoMoi.Text = nomoi.ToString();
                    dgvHoaDonBanSach.Rows.RemoveAt(dgvHoaDonBanSach.SelectedCells[0].RowIndex);
                }
            }
        }

        
    }
}
